package com.jz.community.sharesdk.share;

import android.app.Activity;
import android.graphics.Bitmap;

import com.jz.community.basecomm.bean.ShareInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.constant.Constant;
import com.jz.community.basecomm.utils.Preconditions;
import com.umeng.socialize.Config;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMMin;
import com.umeng.socialize.media.UMWeb;

/**
 * Created by Tony.Fan on 2018/4/2 17:23
 * 分享、微信、朋友圈、QQ
 */
public class ShareApi {

    //分享平台
    public static final int SHARE_WEIXIN_TYPE = 1; //微信
    public static final int SHARE_WEIXIN_CIRCLE_TYPE = 2; //微信朋友圈

    private static ShareApi shareApi = new ShareApi();

    public static ShareApi getInstance() {
        return shareApi;
    }

    /**
     * 分享QQ
     *
     * @param activity
     */
    public void shareQQ(Activity activity, ShareInfo shareInfo) {
        new ShareAction(activity)
                .setPlatform(SHARE_MEDIA.QQ)//传入平台
                .withSubject(shareInfo.getSubject())
                .withText(shareInfo.getText())//分享内容
                .withMedia(new UMImage(activity, shareInfo.getImageUrl()))
                .setCallback(new ShareListener(activity))//回调监听器
                .share();
    }

    /**
     * 分享微信
     *
     * @param activity
     */
    public void shareWeiXin(Activity activity, ShareInfo shareInfo) {

        if (Preconditions.isNullOrEmpty(shareInfo)) {
            return;
        }

        if (!Preconditions.isNullOrEmpty(shareInfo.getBitmap())) {
            UMImage umImage = new UMImage(activity, shareInfo.getBitmap());
            umImage.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
            umImage.setThumb(umImage);
            new ShareAction(activity).withMedia(umImage)
                    .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                    .setCallback(new ShareListener(activity))//回调监听器
                    .share();
        } else {
            UMWeb web = new UMWeb(shareInfo.getLink());
            web.setTitle(shareInfo.getSubject());
            web.setDescription(shareInfo.getText());
            if (!Preconditions.isNullOrEmpty(shareInfo.getImageUrl())) {
                web.setThumb(new UMImage(activity, shareInfo.getImageUrl()));
            }
            new ShareAction(activity).withMedia(web)
                    .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                    .setCallback(new ShareListener(activity))//回调监听器
                    .share();
        }
    }

    /**
     * 分享微信朋友圈
     *
     * @param activity
     */
    public void shareWeiXinCircle(Activity activity, ShareInfo shareInfo) {

        if (Preconditions.isNullOrEmpty(shareInfo)) {
            return;
        }

        if (!Preconditions.isNullOrEmpty(shareInfo.getBitmap())) {
            new ShareAction(activity).withMedia(new UMImage(activity, shareInfo.getBitmap()))
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                    .setCallback(new ShareListener(activity))//回调监听器
                    .share();
        } else {
            UMWeb web = new UMWeb(shareInfo.getLink());
            web.setTitle(shareInfo.getSubject());
            web.setDescription(shareInfo.getText());
            if (!Preconditions.isNullOrEmpty(shareInfo.getImageUrl())) {
                web.setThumb(new UMImage(activity, shareInfo.getImageUrl()));
            }
            new ShareAction(activity).withMedia(web)
                    .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)//传入平台
                    .setCallback(new ShareListener(activity))//回调监听器
                    .share();
        }
    }

    /**
     * 分享微信好友小程序
     *
     * @param activity
     * @param shareInfo
     */
    public void shareWeiXinSmallRoutine(Activity activity, ShareInfo shareInfo) {
        Config.setMiniPreView();
        //兼容低版本的网页链接
        UMMin umMin = new UMMin(Constant.HTTP_URL_BASE_HEAD);
        // 小程序消息封面图片
        umMin.setThumb(new UMImage(activity, shareInfo.getImageUrl()));
        // 小程序消息title
        umMin.setTitle(shareInfo.getSubject());
        // 小程序消息描述
        umMin.setDescription(shareInfo.getText());
        //小程序页面路径
        umMin.setPath(shareInfo.getLink());
        // 小程序原始id,在微信平台查询
        umMin.setUserName(AppConstants.WX_SMALL_ROUTINE_ID);
        new ShareAction(activity).withMedia(umMin)
                .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                .setCallback(new ShareListener(activity))//回调监听器
                .share();
    }
}
